<?php
namespace app\sysAdmin\controller;
use app\common\Base;
use app\common\Compent;
use app\common\Emodel;
use think\Db;

class WorkApprovalConf extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		if(request()->isPost()){
			$page = input('page');
			$rows = input('rows');
			$result = Db::table("T_WORK_APPROVAL_CONF")->select();
			$grid['rows'] = $result;
			$grid['total'] = count($result);
			return $grid;
		}
	}

	public function addUpdate() {
		$all = request()->param();
		try {
			if(array_key_exists("CCER_ID", $all)) {
				$ccerId = $all["CCER_ID"];
				if($ccerId) {
					$where["ID"] = array("IN",explode(",", $all["CCER_ID"]));
					$result = Db::table("教师信息")
						  ->field("ID,姓名 name")
						  ->where($where)
						  ->select();
					$ccerId = [];
					$ccer = [];
					foreach ($result as $key => $value) {
						$ccerId[$key] = $value["ID"];
						$ccer[$key] = $value["name"];
					}
					$all["CCER_ID"] = implode(",", $ccerId);
					$all["CCER"] = implode(",", $ccer);
				}else {
					$all["CCER_ID"] = "";
					$all["CCER"] = "";
				}
			}
			if(array_key_exists("APPROVALER_ID", $all)) {
				$approvalerId = $all["APPROVALER_ID"];
				if($approvalerId) {
					$where["ID"] = array("IN",explode(",", $all["APPROVALER_ID"]));
					$result = Db::table("教师信息")
						  ->field("ID,姓名 name")
						  ->where($where)
						  ->select();
					$approvalerId = [];
					$approvaler = [];
					foreach ($result as $key => $value) {
						$approvalerId[$key] = $value["ID"];
						$approvaler[$key] = $value["name"];
					}
					$all["APPROVALER_ID"] = implode(",", $approvalerId);
					$all["APPROVALER"] = implode(",", $approvaler);
				}else {
					$all["APPROVALER_ID"] = "";
					$all["APPROVALER"] = "";
				}
			}
			if(array_key_exists("ITEM_CODE", $all)) {
				$result = $this::confType();
				foreach ($result as $obj) {
					if($obj["value"] == $all["ITEM_CODE"]) {
						$all["ITEM_NAME"] = $obj["text"];
						break;
					}
				}
			}
			if(array_key_exists("ID", $all)) {
				$all["UPDATE_TIME"] = date("Y-m-d H:i:s");
				update('T_WORK_APPROVAL_CONF', $all);
			} else {
				insert('T_WORK_APPROVAL_CONF', $all);
			}
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function del() {
		$delIds = input("delIds");
		try {
			Db::table('T_WORK_APPROVAL_CONF')->where('ID','in',explode(",", $delIds))->delete();
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

	public function confType() {
		$data = array(
			array("value"=>"printingPaper","text"=>"打印纸审批"),
			array("value"=>"copyPaper","text"=>"复印油纸审批"),
			array("value"=>"teachingGoods","text"=>"教学用品审批"),
			array("value"=>"overtimeApply","text"=>"加班申请审批"),
			array("value"=>"schoolBus","text"=>"校车审批"),
			array("value"=>"teacherLeave","text"=>"教师请假"),
			array("value"=>"officeApproval","text"=>"公文审批"),
			array("value"=>"roomApproval","text"=>"功能室审批"));
		return $data;
	}

}
